; Dit is een 8-bit teller weergegeven op de D poort.
;Assembler directives:

	LIST      p=16f877a            ; List directive to define processor
	INCLUDE   "P16F877A.inc"        ; Processor specific variable definitions
	
	__CONFIG	_CP_OFF & _DEBUG_OFF & _LVP_OFF & _PWRTE_ON & _WDT_OFF & _BODEN_OFF & _HS_OSC

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The labels following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.
;*************************************************************************************************************************
;Memory definition:

Ram		EQU	h'20'		; Look datasheets gpr starts at file address 20h
Count		EQU	Ram+0
Timer_local	EQU	Ram+1
;*************************************************************************************************************************
;Main program:

	ORG	0		; Reset vector
	CALL	Init		; Setup hardware input/output

Reset	CLRF	Count		; Reset Count
Loop	MOVFW	Count		; Move Count into W
	MOVWF	PORTD		; Write W to portd

	CALL	Wait1Second	; Call the 1 second subroutine
	
	INCF	Count,f		; Count=Count+1

	MOVLW	d'255'		; W=16
	XORWF	Count,w		; W=16 XOR Count
	BTFSS	STATUS, Z	; Check the Z flag in the STATUS register
				; Is it set?
	GOTO	Loop		;	No, so count <> 16    keep counting
	GOTO	Reset		;	Yes, so count = 16    reset counter
;*************************************************************************************************************************	
;Subroutines:

Init	CLRF	PORTD		; All of portd low
	BSF	STATUS, RP0	; Change to bank1
	CLRF	TRISD		; All of portd outputs
	BCF	STATUS, RP0	; Back to bank0
	RETURN

Wait1Second	MOVLW	d'250'	; 250ms Merk op juiste waardes hangen af van gebruikt kristal...
		CALL	WaitNms	;
		MOVLW	d'250'	; 250ms
		CALL	WaitNms	;
		MOVLW	d'250'	; 250ms
		CALL	WaitNms	;
		MOVLW	d'250'	; 250ms
		GOTO	WaitNms	;


WaitNms	MOVFW	Timer_local	; Timer_local=W
LoopNms	MOVLW	d'248'		;
	CALL	Loop1ms		;
	NOP			;
	DECFSZ	Timer_local,f	; Dec loop varialble and check
	GOTO	LoopNms		;	No, keep looping
	RETURN			;	Yes, Nms done

Loop1ms	ADDLW	d'255'		; Dec W
	BTFSS	STATUS, Z	; Zero flag set?
	GOTO 	Loop1ms		;	No, keep looping
	RETURN			;	Yes, 1ms done
;*************************************************************************************************************************
	END			; End source code
				; Kristof Goris

		